// pages/message/message.js
let http = require('../../utils/api.js');
let domain = require('../../utils/api.js');
import {
    toData,
    myHttps
} from '../../utils/util.js';
import {
    myHttpss
} from '../../utils/json.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        themeId: '', //主题id
        writecomments: false, //弹框隐藏
        huii: false, //直接聚焦
        list: [], //所有留言
        val: '', //s输入框的内容
        send: '',
        cid: '', //回复评论回复id
        touid: '', //被回复用户的id
        fid: '',
        hiddenLoading: false,
        page: 1,
        size: 10,
        winHeight: "", //窗口高度
        currentTab: 0, //预设当前项的值
        scrollLeft: 0, //tab标题的滚动条位置
        isAcatar: '', //判断是否是此主题的作者
        userId: '', //用户id
        userInfo: '', //用户详情
        domain: '', //ip
        scrollTop:'',
        hideHeader: true,
        hideBottom: true,
        refreshTime: '', // 刷新的时间 
        loadMoreData: '加载更多……'
    },
    // 滚动切换标签样式
    switchTab: function(e) {
        this.setData({
            list: [],
            currentTab: e.detail.current,
            hiddenLoading: false,
            page:1,
            size:10
        });
        this.checkCor();
        if (this.data.isAcatar == 'true') { //是作者 显示审核待审核
            if (e.detail.current == 0) {
                this.getmessage(this.data.page, this.data.size)
            } else if (e.detail.current == 1) {
                this.getMessageAudit(this.data.themeId, this.data.page, this.data.size)
            }
        } else if (this.data.isAcatar == 'false') { //不是作者显示全部留言 
            if (e.detail.current == 0) {
                this.getmessage(this.data.page, this.data.size)
            } else if (e.detail.current == 1) {
                this.myMessage(this.data.themeId, this.data.page, this.data.size)
            }
        }
    },
    // 点击标题切换当前页时改变样式
    swichNav: function(e) {
        var cur = e.target.dataset.current;
        if (this.data.currentTaB == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function() {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(e) {
        // console.log(e.isAcatar)
        this.setData({
            isAcatar: e.isAcatar, //判断是否是作者
            themeId: e.id,
            userId: wx.getStorageSync('userId'), //用户id
            domain: domain.domain
        })
        this.GetInfo(); //用户详情
        var that = this;
        //  高度自适应
        wx.getSystemInfo({
            success: function(res) {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR - 180;
                // console.log(calc)
                that.setData({
                    winHeight: calc
                });
            }
        });

        if (e.isAcatar == 'true') { //是作者 显示审核待审核
            this.getmessage(this.data.page, this.data.size)
        } else if (e.isAcatar == 'false') { //不是作者显示全部留言 
            this.getmessage(this.data.page, this.data.size)
        }
    },
    // 留言点赞
    goods(e) {
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        // console.log(id)
        let url = http.api.commentGoods + id + '/like/topic';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                this.data.list[index].isLike = true
                this.data.list[index].like_count = this.data.list[index].like_count + 1
                this.setData({
                    list: this.data.list,
                    hiddenLoading: true
                })
                // this.getmessage()
            }
        }).catch(err => {})
    },
    //留言取消点赞
    nogoods(e) {
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        let url = http.api.commentGoods + id + '/like/topic';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.data.list[index].isLike = false
                this.data.list[index].like_count = this.data.list[index].like_count - 1
                this.setData({
                    list: this.data.list,
                    hiddenLoading: true
                })
                // this.getmessage()
            }
        }).catch(err => {})
    },
    // 下拉刷新
    // refresh: function (e) {
    //     var that = this;
    //     setTimeout(function () {
    //         var date = new Date();
    //         that.setData({
    //             size: 10,
    //             page: 1,
    //             scrollTop: 0,
    //             refreshTime: date.toLocaleTimeString(),
    //             hideHeader: false
    //         })
    //         if (that.data.isAcatar == 'true') { //是作者 显示审核待审核
    //             if (that.data.currentTab == 0) {
    //                 that.getmessage(that.data.page, that.data.size)
    //             } else if (that.data.currentTab == 1) {
    //                 that.getMessageAudit(that.data.themeId, that.data.page, that.data.size)
    //             }
    //         } else if (that.data.isAcatar == 'false') { //不是作者显示全部留言 
    //             if (that.data.currentTab == 0) {
    //                 that.getmessage(that.data.page, that.data.size)
    //             } else if (that.data.currentTab == 1) {
    //                 that.myMessage(that.data.themeId, that.data.page, that.data.size)
    //             }
    //         }
    //     }, 300);
    // },
    // 上拉加载更多
    loadMore: function () {
        var that = this;
        this.setData({
            page:this.data.page + 1,
            size:10
        })
        setTimeout(function () {
            if (that.data.isAcatar == 'true') { //是作者 显示审核待审核
                if (that.data.currentTab == 0) {
                    that.getmessage(that.data.page, that.data.size)
                } else if (that.data.currentTab == 1) {
                    that.getMessageAudit(that.data.themeId, that.data.page, that.data.size)
                }
            } else if (that.data.isAcatar == 'false') { //不是作者显示全部留言 
                if (that.data.currentTab == 0) {
                    that.getmessage(that.data.page, that.data.size)
                } else if (that.data.currentTab == 1) {
                    that.myMessage(that.data.themeId, that.data.page, that.data.size)
                }
            }
           
        }, 300);
    },
    scroll: function (event) {
        this.setData({
            scrollTop: event.detail.scrollTop
        });
    }, 
    // 获取所有留言接口
    getmessage(page, size, types) {
        // console.log(this.data.themeId)
        let id = this.data.themeId
        let url = http.api.MessageQuery + id + '/comments' + '?page=' + page + '&size=' + size;
        var that = this
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                var arrs = res.data.content;
                if (types) {
                    arrs = arrs.reverse()
                }
                for (var i = 0; i < arrs.length; i++) {
                    res.data.content[i].insert_time = toData(res.data.content[i].insert_time)
                    if (types) {
                        that.data.list.unshift(res.data.content[i])
                    } else {
                        that.data.list.push(res.data.content[i])
                    }

                }
                this.setData({
                    list: that.data.list,
                    hiddenLoading: true,
                    hideHeader: true
                })
            }
        }).catch(err => {})
    },
    // 获取待审核的列表
    getMessageAudit(id, page, size) {
        let url = http.api.getMessageAudit + this.data.userId + '/topic/' + id + '/comments' + '?page=' + page + '&size=' + size;
        var that = this
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                // console.log(res.data.content)
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].insert_time = toData(res.data.content[i].insert_time)
                    that.data.list.push(res.data.content[i])
                }
                that.setData({
                    list: that.data.list,
                    hiddenLoading: true,
                    hideHeader: true
                })
                // console.log(that.data.list)
            }
        }).catch(err => {})
    },
    // 我的留言列表
    myMessage(id, page, size) {
        let url = http.api.myMessage + this.data.userId +  '/comments/topic/' + id + '?page=' + page + '&size=' + size;
        var that = this
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                // console.log(res.data.content)
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].insert_time = toData(res.data.content[i].insert_time)
                    that.data.list.push(res.data.content[i])
                }
                that.setData({
                    list: that.data.list,
                    hiddenLoading: true,
                    hideHeader: true
                })
                // console.log(that.data.list)
            }
        }).catch(err => {})
    },
    // 删除我的留言
    delMessage(e) {
        this.setData({
            hiddenLoading: false
        })
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        let url = http.api.delMessage + this.data.userId + '/comment/topic/' + id
        var that = this
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                that.data.list.splice(index, 1); //删除点击的那一项
                that.setData({
                    list: that.data.list,
                    hiddenLoading: true
                })
                // console.log(that.data.list)
            }
        }).catch(err => {})
    },
    // 点击 忽略或通过走接口
    checkMsg(id, status) {
        let url = http.api.checkMsg + id + '/topic'
        var that = this
        let obj = {
            status: status
        }
        myHttps({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    list: []
                })
                this.getMessageAudit(this.data.themeId, this.data.page, this.data.size)
            }
        }).catch(err => {})
    },
    // 点击忽略
    neglect(e) {
        let id = e.currentTarget.dataset.id
        this.checkMsg(id, 'Invalid')
    },
    // 点击通过
    pass(e) {
        let id = e.currentTarget.dataset.id
        this.checkMsg(id, 'Approved')
    },
    // 留言回复
    replyMes(e) {
        // console.log(e)
        this.setData({
            writecomments: true, //弹框出现
            huii: true, //自动聚焦
            send: 2,
            fid: e.currentTarget.dataset.id,
            touid: e.currentTarget.dataset.touid
        })
    },
    // 点击写评论
    writeComment() {
        this.setData({
            huii: true, //自动聚焦 
            send: 1,
            writecomments: true, //弹框出现
        })
    },
    //关闭输入弹框
    closebg() {
        this.setData({
            writecomments: false
        })
    },
    // 点击发送
    send(){
        if (this.data.isAcatar == 'true'){
            this.sendAvatar()
        }else {
            this.sendS()
        }
    },
    // 点击完成提交数据走接口 不是作者的时候
    sendS() {
        // console.log(this.data.currentTab) 
        let id = this.data.themeId
        // send=1 走写留言接口
        if (this.data.send == 1) {
            let url = http.api.themeMessage + id + '/comments';
            let obj = {
                content: this.data.val
            }
            myHttpss({
                url: url,
                method: "POST",
                data: obj
            }).then(res => {
                if (res.code == 1) {
                    if (this.data.currentTab == 1) {//如果再我的留言页面下面写留言 就显示 如果在全部留言下面写留言 就先不显示 切换到我的留言下面再显示
                        this.data.list.splice(0, 9);
                        this.setData({
                            list: this.data.list,
                            comment: true,
                            hiddenLoading: true
                        })
                        this.myMessage(this.data.themeId, 1, 10, 'u')
                    }
                }
            }).catch(err => {})
        } else if (this.data.send == 2) {
            // send=2 走回复留言接口
            let url = http.api.replyMes + id + '/comments';
            let obj = {
                content: this.data.val,
                to_uid: this.data.touid,
                f_id: this.data.fid,
            }
            // console.log(obj)
            myHttpss({
                url: url,
                method: "POST",
                data: obj
            }).then(res => {
                if (res.code == 1) {
                    if (this.data.currentTab == 1) {//如果再我的留言页面下面写留言 就显示 如果在全部留言下面写留言 就先不显示 切换到我的留言下面再显示
                        this.data.list.splice(0, 9);
                        this.setData({
                            list: this.data.list,
                            comment: true,
                            hiddenLoading: true
                        })
                        this.myMessage(this.data.themeId,1, 10, 'u')
                    }
                }
            }).catch(err => {})
        }

    },
    // 点击完成提交数据走接口 是作者时候
    sendAvatar() {
        console.log(this.data.currentTab)
        let id = this.data.themeId
        // send=1 走写留言接口
        if (this.data.send == 1) {
            let url = http.api.themeMessage + id + '/comments';
            let obj = {
                content: this.data.val
            }
            myHttpss({
                url: url,
                method: "POST",
                data: obj
            }).then(res => {
                if (res.code == 1) {
                    if (this.data.currentTab == 0) {//如果是自己得留言 就是显示在已审核中
                        this.data.list.splice(0, 9);
                        this.setData({
                            list: this.data.list,
                            comment: true,
                            hiddenLoading: true
                        })
                        console.log(this.data.list)
                        this.getmessage(1, 10, 'u')
                    }

                }
            }).catch(err => { })
        } else if (this.data.send == 2) {
            // send=2 走回复留言接口
            let url = http.api.replyMes + id + '/comments';
            let obj = {
                content: this.data.val,
                to_uid: this.data.touid,
                f_id: this.data.fid,
            }
            // console.log(obj)
            myHttpss({
                url: url,
                method: "POST",
                data: obj
            }).then(res => {
                if (res.code == 1) {
                    if (this.data.currentTab == 0) {//如果再我的留言页面下面写留言 就显示 如果在全部留言下面写留言 就先不显示 切换到我的留言下面再显示
                        this.data.list.splice(0, 9);
                        this.setData({
                            list: this.data.list,
                            comment: true,
                            hiddenLoading: true
                        })
                        this.getmessage(1, 10, 'u')
                    }
                }
            }).catch(err => { })
        }

    },
    //输入字变色
    shuru(e) {
        let val = e.detail.value;
        this.setData({
            val: val
        })
        // console.log(this.data.val)
        if (e.detail.value == '') {
            this.setData({
                xianshi: false
            });
        } else {
            this.setData({
                xianshi: true
            });
        }
    },
    // 获取用户信息接口 
    GetInfo() {
        var url = http.api.userInfo + this.data.userId;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    userInfo: res.data
                })
            }
        }).catch(err => {})
    },
})